package org.miyuki4j.cfgtool;

import lombok.extern.slf4j.Slf4j;

import java.io.Closeable;
import java.util.function.BiConsumer;

/**
 * @Desc
 * @Author miyuki4j
 * @Date 2025/7/25/星期五 10:52
 * @Version 1.0
 */
@Slf4j
public class IOUtils {

    public static void closeQuietly(final Closeable closeable) {
        closeQuietly(closeable, (c, e) -> log.error("error closing resource={}", c, e));
    }

    public static void closeQuietly(final Closeable closeable, final BiConsumer<Closeable, Exception> closeExceptionCatcher) {
        if (closeable == null) {
            return;
        }
        try {
            closeable.close();
        } catch (Exception exc) {
            if (closeExceptionCatcher != null) {
                closeExceptionCatcher.accept(closeable, exc);
            } else {
                log.error("error closing resource={}", closeable, exc);
            }
        }
    }
}
